from flask import Blueprint, request, jsonify
from .utils import execute_program
import os

main = Blueprint('main', __name__)


@main.route('/run', methods=['POST'])
def run_gpu_program():
    """接收文件，执行并返回GPU计算结果"""
    file = request.files['file']
    filename = "gpu_program" + (".exe" if os.name == 'nt' else "")
    file_path = "./" + filename
    file.save(file_path)

    # 使用utils模块中的函数执行程序
    success, output = execute_program(file_path)

    if success:
        return jsonify({"output": output}), 200
    else:
        return jsonify({"error": output}), 400
